<template>
  <div class="contain">
    <div class="left">{{ formatcurrenttime }}</div>
    <div class="middle" @click="middleclick">
      <div class="back" ref="backref"></div>
      <div class="progress" ref="progressref" :style="progressstyle"></div>
      <div
        class="button"
        :style="buttonstyle"
        @touchstart="touchstart"
        @touchmove.prevent="touchmove"
        @touchend="touchend"
        ref="buttonref"
      ></div>
    </div>
    <div class="right">{{ alltime }}</div>
  </div>
</template>

<script>
import { ref, watch, computed, onMounted } from 'vue'
import { FormatTime } from '@/utils'
export default {
  name: 'Progress',
  props: {
    currentsong: Object,
    progressvalue: Number
  },
  emits: ['changetime'],
  setup (props, { emit }) {
    const currenttime = ref(0)
    const alltime = ref(0)
    const backref = ref(null)
    const progressref = ref(null)
    const buttonref = ref(null)
    const formatcurrenttime = computed(() => FormatTime(currenttime.value))
    watch(
      () => props.currentsong,
      (newv) => {
        currenttime.value = 0
        emit('changetime', 0)
        alltime.value = FormatTime(newv.duration)
      }
    )
    onMounted(() => {
      alltime.value = FormatTime(props.currentsong.duration)
    })
    watch(
      () => props.progressvalue,
      (newv) => {
        currenttime.value =
          (props.progressvalue * props.currentsong.duration) | 0
      }
    )
    // 进度条样式
    const progressstyle = computed(() => {
      if (!backref.value) return
      const width = props.progressvalue * backref.value.offsetWidth
      return {
        width: `${width}px`
      }
    })
    // 圆点样式
    const buttonstyle = computed(() => {
      if (!backref.value) return
      const left = props.progressvalue * backref.value.offsetWidth - 11
      return {
        left: `${left}px`
      }
    })
    // 监听拖动
    const touch = { x1: 0, offset: 0 }
    const touchstart = (e) => {
      touch.x1 = e.touches[0].pageX
    }
    const touchmove = (e) => {
      const offset =
        (e.touches[0].pageX - backref.value.getBoundingClientRect().left) /
        backref.value.offsetWidth
      touch.offset = Math.max(0, Math.min(1, offset))
      emit('touchmovef', touch.offset)
    }
    const touchend = () => {
      emit('changetime', touch.offset)
    }
    // 点击改变进度条
    const middleclick = (e) => {
      const offset =
        (e.pageX - progressref.value.getBoundingClientRect().left) /
        backref.value.offsetWidth
      touch.offset = Math.max(0, Math.min(1, offset))
      emit('changetime', touch.offset)
    }
    return {
      formatcurrenttime,
      alltime,
      backref,
      buttonstyle,
      progressstyle,
      touchstart,
      touchmove,
      touchend,
      progressref,
      buttonref,
      middleclick
    }
  }
}
</script>

<style scoped lang="scss">
.contain {
  position: relative;
  bottom: 150px;
  width: 80%;
  margin: 0 auto;
  padding: 10px 0;
  display: flex;
  .left,
  .right {
    width: 40px;
    text-align: left;
    font-size: 12px;
    height: 30px;
    line-height: 30px;
  }
  .right {
    text-align: right;
  }
  .middle {
    flex: 1;
    position: relative;
    .back {
      position: absolute;
      width: 100%;
      background: rgba(0, 0, 0, 0.3);
      height: 4px;
      top: 14px;
    }
    .progress {
      position: absolute;
      background-color: #ffcd32;
      height: 4px;
      top: 14px;
    }
    .button {
      position: absolute;
      width: 16px;
      height: 16px;
      background-color: #ffcd32;
      border: 3px solid white;
      left: 0;
      top: 6px;
      border-radius: 50%;
    }
  }
}
</style>
